<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
  <meta content="" name="keywords">

  <title> Django - </title>
  <meta name="description" content="" />
  <meta property="og:title" content="Django" />
  <meta name="twitter:title" content="Django" />
  <meta name="author" content="{Description { .Site.Author.name }}"/>
  <meta name="twitter:card" content="summary" />
  <meta property="og:url" content="https://systemime.github.io/categories/django/" />
  <meta property="og:type" content="website" />
  <meta property="og:site_name" content="听风的小站" />

  <meta name="generator" content="Hugo 0.67.0" />
  <link rel="canonical" href="https://systemime.github.io/categories/django/" />
  <link rel="alternate" href="https://systemime.github.io/index.xml" type="application/rss+xml" title="听风的小站">
  <script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/jquery-1.12.4.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" integrity="sha384-wITovz90syo1dJWVh32uuETPVEtGigN07tkttEqPv+uR2SE/mbQcG7ATL28aI9H0" crossorigin="anonymous">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  
  
  <link rel="stylesheet" href="https://systemime.github.io/css/main.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/my.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/pace.css" />
  <link rel="stylesheet" href="https://cdn.plyr.io/3.4.7/plyr.css">
  <link rel="stylesheet" href="https://systemime.github.io/css/lightgallery.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/baguetteBox.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/search.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/reward.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/share.min.css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" />
  <link rel="stylesheet" href="https://systemime.github.io/css/pygment_highlights.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/highlight.min.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/toc.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/lightbox.css" />
  <link rel="stylesheet" href="https://systemime.github.io/live2d/css/live2d.css" />
  
  <link href="https://systemime.github.io/css/video-js.css" rel="stylesheet">


<meta name="baidu-site-verification" content="g8IYR9SNLF" />
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cffe439e37449bb1c07ab26ab56484bb";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

<link rel="stylesheet" href="https://systemime.github.io/css/prism.css" />









<script src="https://systemime.github.io/js/pace.min.js" data-no-instant></script>



<script src="https://systemime.github.io/js/instantclick.min.js" data-no-instant></script>
   <script data-no-instant>
   InstantClick.on('change', function(isInitialLoad) {
     if (isInitialLoad === false) {
       if (typeof MathJax !== 'undefined') 
         MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
       if (typeof prettyPrint !== 'undefined') 
         prettyPrint();
       if (typeof _hmt !== 'undefined')  
         _hmt.push(['_trackPageview', location.pathname + location.search]);
       if (typeof ga !== 'undefined')  
           ga('send', 'pageview', location.pathname + location.search);
     }
   });
   InstantClick.init();
</script>




<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gist-embed/2.7.1/gist-embed.min.js"></script>


<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
  (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: "ca-pub-3925981084585036",
    enable_page_level_ads: true
  });
</script>



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/photoswipe.min.css" integrity="sha256-sCl5PUOGMLfFYctzDW3MtRib0ctyUvI9Qsmq2wXOeBY=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/default-skin/default-skin.min.css" integrity="sha256-BFeI1V+Vh1Rk37wswuOYn5lsTcaU96hGaI7OUVCLjPc=" crossorigin="anonymous" />



<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

<div class="pswp__bg"></div>

<div class="pswp__scroll-wrap">
    
    <div class="pswp__container">
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
    </div>
    
    <div class="pswp__ui pswp__ui--hidden">
    <div class="pswp__top-bar">
      
      <div class="pswp__counter"></div>
      <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
      <button class="pswp__button pswp__button--share" title="Share"></button>
      <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
      <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
      
      
      <div class="pswp__preloader">
        <div class="pswp__preloader__icn">
          <div class="pswp__preloader__cut">
            <div class="pswp__preloader__donut"></div>
          </div>
        </div>
      </div>
    </div>
    <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
      <div class="pswp__share-tooltip"></div>
    </div>
    <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
    </button>
    <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
    </button>
    <div class="pswp__caption">
      <div class="pswp__caption__center"></div>
    </div>
    </div>
    </div>
</div>

</head>

  <body>
    <nav class="navbar navbar-default navbar-fixed-top navbar-custom">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="https://systemime.github.io">听风的小站</a>
    </div>
    <div class="collapse navbar-collapse" id="main-navbar">
      <ul class="nav navbar-nav navbar-right">
        
          
            <li>
		    <a title="博客" href="https://systemime.github.io/blog/">博客</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="片刻" href="https://systemime.github.io/moment/">片刻</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="关于" href="https://systemime.github.io/about/">关于</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="留言" href="https://systemime.github.io/message/">留言</a>
              
              
            </li>
          
        

        

        
        
      </ul>
    </div>

    <div class="avatar-container">
      <div class="avatar-img-border">
        
      </div>
    </div>

  </div>
</nav>






    
  <header class="header-section ">
    <div class="intro-header no-img">
      <div class="container">
        <div class="row">
          <div class="col-lg-12 col-md-12 col-md-offset-0">
            <div class="page-heading">
              <h1>#Django</h1>
              <hr class="small">
            </div>
          </div>
        </div>
      </div>
    </div>
  </header>

    
  <div class="container" role="main">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        
        <div class="posts-list">
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-04/django%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%BD%91%E9%A1%B5%E5%BC%B9%E5%87%BA%E8%AD%A6%E5%91%8A/">
                <h2 class="post-title">Django实现简单网页弹出警告</h2>

                
              </a>

              <p class="post-meta">
                Posted on April 6, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="功能所在位置">功能所在位置</h2>
<p>该功能运用了django.contrib.messages这个库，再django项目中srtting.py文件中的38行，APP注册部分</p>
<pre><code>INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01.apps.App01Config',  # 自定义APP注册
]
</code></pre><h2 id="导入库">导入库</h2>
<pre><code>from django.contrib import messages

def abc(request):
    messages.success(request, &quot;哈哈哈&quot;)
</code></pre><h2 id="前台代码添加">前台代码添加</h2>
<p>需在html页面上显示这个消息提示框，代码如下，但必须注意的是如果页面有多个表单的时候，只需要在表单结束后加一次就够了，view底下不同的函数可以显示不同的消息：</p>
<pre><code>{% if messages %}
    &lt;script&gt;
        {% for msg in messages %}
            alert('{{ msg.message }}');
        {% endfor %}
    &lt;/script&gt;
{% endif %}
</code></pre>
                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/django/">Django</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/messages/">messages</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E5%BC%B9%E7%AA%97/">弹窗</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-04/django%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E5%88%97%E7%9A%84%E6%95%B0%E6%8D%AE/">
                <h2 class="post-title">Django获取指定列的数据</h2>

                
              </a>

              <p class="post-meta">
                Posted on April 6, 2019
              </p>
              <div class="post-entry">
                
                  <p>model一般都是有多个属性的，但是很多时候我们又只需要查询特定的某一个，这个时候可以用到<code>values</code>和<code>values_list</code></p>
<h2 id="二者简介">二者简介</h2>
<pre><code>[values()](https://docs.djangoproject.com/en/1.9/ref/models/querysets/#values)
values()¶

values(*fields)¶
Returns a QuerySet that returns dictionaries, rather than model instances, when used as an iterable.

Each of those dictionaries represents an object, with the keys corresponding to the attribute names of model objects.
</code></pre><pre><code>[values_list()](https://docs.djangoproject.com/en/1.9/ref/models/querysets/#values-list)
values_list()¶

values_list(*fields, flat=False)¶
This is similar to values() except that instead of returning dictionaries, it returns tuples when iterated over. Each tuple contains the value from the respective field passed into the values_list() call — so the first item is the first field, etc.
</code></pre><h2 id="values查询">values查询</h2>
<pre><code>from attendence.models import Employee
from attendence.models import EmployeeIP

#获取一个字段
ipList = EmployeeIP.objects.values(&quot;IP&quot;).first()
print(type(ipList))
# &lt;class 'dict'&gt;
print(ipList)
# {'IP': '192.168.1.41'}

#获取多个字段
empList = Employee.objects.values(&quot;first_name&quot;, &quot;last_name&quot;, &quot;email&quot;)[0:2]
print(type(empList))
# &lt;class 'django.db.models.query.QuerySet'&gt;
print(empList)
# [
#   {'last_name': 'Wei', 'first_name': 'Vena', 'email': 'Vena@test.com'},
#   {'last_name': 'Wan', 'first_name': 'Mark', 'email': 'mwan@test.com'}
# ]
</code></pre><h2 id="values_list查询">values_list查询</h2>
<pre><code>ipList = EmployeeIP.objects.values_list(&quot;IP&quot;).first()
print(type(ipList))
# &lt;class 'tuple'&gt;
print(ipList)
# ('192.168.1.111',)

ipList = EmployeeIP.objects.values_list(&quot;IP&quot;)[0:2]
print(type(ipList))
# &lt;class 'django.db.models.query.QuerySet'&gt;
print(ipList)
# [('192.168.1.41',), ('192.168.1.44',)]
print(type(ipList[0]))
# &lt;class 'tuple' &gt;
print(ipList[0])
# 192.168.1.111
</code></pre><h2 id="区别">区别</h2>
<p>从上面的代码中我们可以看到返回结果类型上细微的差别</p>
<ul>
<li>vlaues -
<ul>
<li>单条记录 - <code>&lt;class 'dict'&gt;</code></li>
<li>多条记录 - <code>&lt;class 'django.db.models.query.QuerySet'&gt;</code></li>
</ul>
</li>
<li>vlaues_list -
<ul>
<li>单条记录 - <code>&lt;class 'tuple'&gt;</code></li>
<li>多条记录 - <code>&lt;class 'django.db.models.query.QuerySet'&gt;</code></li>
</ul>
</li>
</ul>

                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/django/">Django</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/models/">models</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-04/%E8%A7%A3%E5%86%B3django2.2%E5%90%AF%E5%8A%A8-str-object-has-no-attribute-decode%E6%8A%A5%E9%94%99%E9%97%AE%E9%A2%98/">
                <h2 class="post-title">解决django2.2启动-str-object-has-no-attribute-decode报错问题</h2>

                
              </a>

              <p class="post-meta">
                Posted on April 6, 2019
              </p>
              <div class="post-entry">
                
                  <p>Django2.2报错&ndash;AttributeError: &lsquo;str&rsquo; object has no attribute &lsquo;decode'的解决方案，仅算是临时解决，所有更好的解决方案应该斟酌使用本方法</p>
<h2 id="报错信息">报错信息</h2>
<pre><code>G:\book_pulisher&gt;python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Exception in thread Thread-1:
Traceback (most recent call last):
  File &quot;C:\ProgramData\Anaconda3\lib\threading.py&quot;, line 916, in _bootstrap_inner
    self.run()
  File &quot;C:\ProgramData\Anaconda3\lib\threading.py&quot;, line 864, in run
    self._target(*self._args, **self._kwargs)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\utils\autoreload.py&quot;, line 54, in wrapper
    fn(*args, **kwargs)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py&quot;, line 120, in inner_run
    self.check_migrations()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\base.py&quot;, line 453, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\executor.py&quot;, line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\loader.py&quot;, line 49, in __init__
    self.build_graph()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\loader.py&quot;, line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\recorder.py&quot;, line 73, in applied_migrations
    if self.has_table():
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\recorder.py&quot;, line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\base\base.py&quot;, line 256, in cursor
    return self._cursor()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\base\base.py&quot;, line 233, in _cursor
    self.ensure_connection()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\base\base.py&quot;, line 217, in ensure_connection
    self.connect()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\base\base.py&quot;, line 197, in connect
    self.init_connection_state()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py&quot;, line 231, in init_connection_state
    if self.features.is_sql_auto_is_null_enabled:
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\utils\functional.py&quot;, line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\mysql\features.py&quot;, line 82, in is_sql_auto_is_null_enabled
    cursor.execute('SELECT @@SQL_AUTO_IS_NULL')
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\utils.py&quot;, line 103, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\mysql\operations.py&quot;, line 146, in last_executed_query
    query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
</code></pre><h2 id="解决方案">解决方案</h2>
<p>我使用的是anaconda python，找到** <code>C:\ProgramData\Anaconda3\Lib\site-packages\django\db\backends\mysql\operations.py</code> **</p>
<p>因为这里是报错的语句，在145行，如下</p>
<pre><code>    def last_executed_query(self, cursor, sql, params):
        # With MySQLdb, cursor objects have an (undocumented) &quot;_executed&quot;
        # attribute where the exact query sent to the database is saved.
        # See MySQLdb/cursors.py in the source distribution.
        query = getattr(cursor, '_executed', None)
        if query is not None:
            query = query.decode(errors='replace')
        return query
</code></pre><p>由此可看到，这里是一个if的判断，功能暂时不讨论，但是目前看是会触发这个if的，<code>decode</code>作为解码函数，那么可不可以直接返回，将145、146的<code>if</code>语句注释掉再尝试</p>
<pre><code>    def last_executed_query(self, cursor, sql, params):
        # With MySQLdb, cursor objects have an (undocumented) &quot;_executed&quot;
        # attribute where the exact query sent to the database is saved.
        # See MySQLdb/cursors.py in the source distribution.
        query = getattr(cursor, '_executed', None)
        # if query is not None:
        #     query = query.decode(errors='replace')
        return query
</code></pre><p>再次启动项目，成功启动</p>
<pre><code>G:\book_pulisher&gt;python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
April 03, 2019 - 20:35:51
Django version 2.2, using settings 'book_pulisher.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[03/Apr/2019 20:35:53] &quot;GET / HTTP/1.1&quot; 404 3049
Not Found: /favicon.ico
[03/Apr/2019 20:35:53] &quot;GET /favicon.ico HTTP/1.1&quot; 404 3100
Not Found: /^publisher_list/
[03/Apr/2019 20:36:00] &quot;GET /%5Epublisher_list/ HTTP/1.1&quot; 404 3117
[03/Apr/2019 20:36:07] &quot;GET /publisher_list/ HTTP/1.1&quot; 200 7384
[03/Apr/2019 20:36:07] &quot;GET /static/dashboard.css HTTP/1.1&quot; 200 1557
[03/Apr/2019 20:36:07] &quot;GET /static/fontawesome/css/font-awesome.min.css HTTP/1.1&quot; 200 32422
[03/Apr/2019 20:36:07] &quot;GET /static/bootstrap/js/bootstrap.min.js HTTP/1.1&quot; 200 37045
[03/Apr/2019 20:36:07] &quot;GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1&quot; 200 121200
[03/Apr/2019 20:36:07] &quot;GET /static/jquery-3.3.1.js HTTP/1.1&quot; 200 271751
[03/Apr/2019 20:36:07] &quot;GET /static/fontawesome/fonts/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1&quot; 200 77160
[03/Apr/2019 20:36:10] &quot;GET /book_list/ HTTP/1.1&quot; 200 7426
</code></pre><h2 id="注意">注意</h2>
<ul>
<li>仅算是临时解决，所有更好的解决方案应该斟酌使用本方法</li>
<li>不要信网上说的，把decode改为encode</li>
</ul>

                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/django/">Django</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E6%8A%A5%E9%94%99/">报错</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-04/django-%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">
                <h2 class="post-title">Django 常用命令</h2>

                
              </a>

              <p class="post-meta">
                Posted on April 5, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="一创建新项目">一、创建新项目</h2>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="n">django</span><span class="o">-</span><span class="n">admin</span><span class="o">.</span><span class="n">py</span> <span class="n">startproject</span> <span class="n">my_project</span></code></pre></div>
<h2 id="二创建app">二、创建App</h2>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="c1"># 在Django项目的根目录下执行</span>
<span class="n">python3</span> <span class="n">manage</span><span class="o">.</span><span class="n">py</span> <span class="n">startapp</span> <span class="n">my_app</span></code></pre></div>
<h2 id="三同步数据库">三、同步数据库</h2>
<pre><code># make new migrations
python3 manage.py makemigrations
 
# apply all migrations
python3 manage.py migrate

</code></pre><h2 id="四启动服务器">四、启动服务器</h2>
<pre><code># run server
python3 manage.py runserver
</code></pre><h2 id="五创建models">五、创建models</h2>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="k">class</span> <span class="nc">App1</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
 
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
 
    <span class="n">category</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
 
    <span class="n">date_time</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">auto_now_add</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
 
    <span class="k">def</span> <span class="fm">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
 
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">title</span>
 
    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
 
        <span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;-date_time&#39;</span><span class="p">]</span>
		</code></pre></div>
<h2 id="六进入django中的交互式shell">六、进入Django中的交互式shell</h2>
<pre><code>python3 manage.py shell
 
from app1.models import App1
 
# create(增加操作)
App1.objects.create(title='title',category='category1')
 
# all和get(查看操作)
App1.objects.all()
App1.objects.get(id=1)
 
# update(修改操作)
demo = App1.objects.get(id=1)
demo.title
demo.title='title2'
 
# delete(删除操作)
demo.delete()
</code></pre><h2 id="七创建一个超级用户">七、创建一个超级用户</h2>
<pre><code>python3 manage.py createsuperuser
 
# 输入http://127.0.0.1:8000/admin登录后台
</code></pre><h2 id="八数据库信息的增加和删除adminpy中增加代码">八、数据库信息的增加和删除，admin.py中增加代码</h2>
<pre><code>from article.models import App1
 
admin.site.register(App1)
</code></pre><h2 id="九将代码上传到github">九、将代码上传到Github</h2>
<pre><code># 在github中新建仓库django-blog
# 在项目根目录下执行
git init
git remote add Django
git@github.com:ybbz/django-blog.git
git add .
git status
git commit -m &quot;django blog init&quot;
git pull Django master &amp;&amp; git push Django master
</code></pre><h2 id="十error-that-port-is-already-in-use">十、Error: That port is already in use.</h2>
<pre><code>ps aux | grep -i manage
kill -9 ****
python3 manage.py runserver
</code></pre>
                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/django/">Django</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E5%91%BD%E4%BB%A4/">命令</a>&nbsp;
                  
                </span>
              

            </article>
          
        </div>
        
          <ul class="pager main-pager">
            
              <li class="previous">
                <a href="https://systemime.github.io/categories/django/page/1">&larr; Newer Posts</a>
              </li>
            
            
              <li class="next">
                <a href="https://systemime.github.io/categories/django/page/3">Older Posts &rarr;</a>
              </li>
            
          </ul>
        
      </div>
    <div align="center" class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        

<ul class="pagination">
    
    <li class="page-item">
        <a href="https://systemime.github.io/categories/django/" class="page-link" aria-label="First"><span aria-hidden="true">&laquo;&laquo;</span></a>
    </li>
    
    <li class="page-item">
    <a href="https://systemime.github.io/categories/django/" class="page-link" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a>
    </li>
    
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/categories/django/">1</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item active"><a class="page-link" href="https://systemime.github.io/categories/django/page/2/">2</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/categories/django/page/3/">3</a></li>
    
    
    <li class="page-item">
    <a href="https://systemime.github.io/categories/django/page/3/" class="page-link" aria-label="Next"><span aria-hidden="true">&raquo;</span></a>
    </li>
    
    <li class="page-item">
        <a href="https://systemime.github.io/categories/django/page/3/" class="page-link" aria-label="Last"><span aria-hidden="true">&raquo;&raquo;</span></a>
    </li>
    
</ul>


        
        
        
        
        
      </div>
    </div>
  </div>

    <div class="rocket"><img src="https://systemime.github.io/img/rocket_up.png" alt="" width="100" height="100"></div>
<script>
    $(function () {
      $(window).scroll(function () {
        
        if ($(window).scrollTop() >= 1000) {
          $('.rocket').stop().fadeIn(1000);
        }else {
          $('.rocket').stop().fadeOut(1000);
        }
      })
      
      $('.rocket').click(function () {
        $('html,body').stop().animate({scrollTop:0},400);
       
      })
    })

</script>

    
    <div id="landlord">
        <div class="message" style="opacity:0"></div>
        <canvas id="live2d" width="280" height="250" class="live2d"></canvas>
        <div class="hide-button">隐藏</div>
    </div>
    
    <footer id="copyright">
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <ul class="list-inline text-center footer-links">
          
          
        </ul>
        <p class="credits copyright text-muted">
        &copy;2017-2018
          
          &nbsp;&bull;&nbsp;
          September 20,2019
          updated
          
            &nbsp;&bull;&nbsp;
            <a href="https://systemime.github.io">Home</a>
          
        </p>

        <p class="credits theme-by text-muted">
        <a href="https://servicemesher.github.io" target="_blank">ServiceMesher</a>&nbsp;&bull;&nbsp;<a href="https://github.com/yangchuansheng/k8s-knowledge" target="_blank">Kubernetes 知识图谱</a>
        <br />
        <span id="busuanzi_container_site_pv">
            本站已被访问 <span id="busuanzi_value_site_pv"></span> 次啦
        </span>
        <span id="busuanzi_container_site_uv">
            &ensp;|&ensp;&thinsp;您是第 <span id="busuanzi_value_site_uv"></span> 位到访的小伙伴喔
        </span>
        </p>
        
        <p class="credits theme-by text-muted">
          <a href="http://gohugo.io">Hugo v0.67.0</a> powered &nbsp;&bull;&nbsp; Theme by <a href="http://deanattali.com/beautiful-jekyll/">Beautiful Jekyll</a> adapted to <a href="https://github.com/halogenica/beautifulhugo">Beautiful Hugo</a>
          
          <center><section class="credits theme-by text-muted">
    <span class="footer__copyright">
    <div>
    <span id="span_dt_dt"> </span>
    <script language="javascript">
      function show_date_time(){
        window.setTimeout("show_date_time()", 1000);
        BirthDay=new Date("7/8/2017 10:56:12");
        today=new Date();
        timeold=(today.getTime()-BirthDay.getTime());
        sectimeold=timeold/1000
        secondsold=Math.floor(sectimeold);
        msPerDay=24*60*60*1000
        e_daysold=timeold/msPerDay
        daysold=Math.floor(e_daysold);
        e_hrsold=(e_daysold-daysold)*24;
        hrsold=Math.floor(e_hrsold);
        e_minsold=(e_hrsold-hrsold)*60;
        minsold=Math.floor((e_hrsold-hrsold)*60);
        seconds=Math.floor((e_minsold-minsold)*60);
        span_dt_dt.innerHTML="本博客已经开心运行 "+daysold+" 天 "+hrsold+" 小时 "+minsold+" 分 "+seconds+" 秒";
      }
      show_date_time();
    </script>
    </div>
</script>
</section>
</center>
        </p>
      </div>
    </div>
  </div>
</footer>


<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/bootstrap.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/photoswipe-ui-default.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/photoswipe.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/auto-render.min.js"></script>
<script src="https://systemime.github.io/js/main.js"></script>
<script src="https://systemime.github.io/js/clipboard.min.js"></script>
<script src="https://systemime.github.io/js/prism.js?t=123"></script>
<script src="https://systemime.github.io/js/highlight.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/katex.min.js"></script>
<script src="https://systemime.github.io/js/reward.js"></script>
<script src="https://systemime.github.io/js/canvas_ribbon.js"></script>
<script> renderMathInElement(document.body); </script>

<script src="https://systemime.github.io/js/baguetteBox.js"></script>
<script> baguetteBox.run('.gallery');</script>






<script async defer src="https://buttons.github.io/buttons.js"></script>


<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
<script type="text/javascript" src="https://systemime.github.io/js/quicklink.umd.js"></script>
<script>
window.addEventListener('load', () =>{
   quicklink();
});
</script>



<script type="text/javascript">
 
var a_idx = 0;
jQuery(document).ready(function($) {
    $("body").click(function(e) {
        var a = new Array("Docker", "Kubernetes", "Prometheus", "Envoy", "Istio", "Service Mesh", "Cloud Native");
        var $i = $("<span />").text(a[a_idx]);
        a_idx = (a_idx + 1) % a.length;
        var x = e.pageX,
        y = e.pageY;
        function randomColor() {
          var flakeColor = new Array("#FFDA65", "#00BFFF", "#BA55D3", "#FFA07A", "#87CEEB", "#FFB6C1");
          var snowColor = flakeColor[Math.floor(flakeColor.length * Math.random())];
          return snowColor;
        }
        $i.css({
            "z-index": 999999999999999999999999999999999999999999999999999999999999999999999,
            "top": y - 20,
            "left": x,
            "position": "absolute",
            "font-weight": "bold",
            "color": randomColor()
        });
        $("body").append($i);
        $i.animate({
            "top": y - 180,
            "opacity": 0
        },
        1500,
        function() {
            $i.remove();
        });
    });
});
</script>


<script type="text/javascript" src="https://systemime.github.io/js/lightbox.js"></script>











<script src="https://cdn.plyr.io/3.4.7/plyr.js"></script>
<script>const player = new Plyr('#player');</script>


<script async defer src="https://buttons.github.io/buttons.js"></script>


  </body>
</html>

